package tugas.akhir.jason;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import tugas.akhir.jason.CustomHttpClient;

import tugas.akhir.jason.login;
import tugas.akhir.jason.MIDDASActivity;
import android.util.Log;
import android.app.ProgressDialog;

public class login extends Activity{
	protected static final int SUB_ACTIVITY_REQUEST_CODE = 100;
	private Button btnLogin;
	private Button btnCancel;
	private EditText username;
	private EditText password;
	private TextView warning;
	private ProgressDialog progressDialog; //Progress Dialog
    protected static final int REQUEST_CODE = 0;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
       
                  	
        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        btnLogin=(Button)findViewById(R.id.btnLogin);
        btnCancel=(Button)findViewById(R.id.btnCancel);
        warning=(TextView)findViewById(R.id.labelWarning);
        
        //Jika memilih button cancel
        btnCancel.setOnClickListener(new View.OnClickListener() {
    		public void onClick(View view) {
    			startActivity(new Intent(login.this, MIDDASActivity.class));
    		}
    	});

        if (AppStatus.getInstance(this).isOnline(this)) {
        	
      //Jika memilih button OK
        btnLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                       	
            	// TODO Auto-generated method stub
            	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            	postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
            	postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
            	
            	//String valid = "1";
            	String response = null;
            	try {
            	    response = CustomHttpClient.executeHttpPost("http://192.168.0.101/rs/assessor.php", postParameters);
            	    String res=response.toString();
            	    res= res.replaceAll("\\s+","");         	              	 
            	    
            	  //jika Nomor Telepon dan Password yang dimasukkan benar
            	  if(res.equals("1"))           	    
            	    { progressDialog = ProgressDialog.show(login.this, "", "Mohon tunggu...");
            	    
            	    	new Thread() {
            		  public void run() {
            		  try{
            			  String nomortlp = username.getText().toString();
            			  String pass = password.getText().toString();
            			  Bundle bundle = new Bundle();
            			  bundle.putString("nomortlp", nomortlp);
            			  bundle.putString("pass", pass);
            			  Intent intent = new Intent(login.this, dr0_mainmenuassessor.class);
            			  intent.putExtras(bundle);
            			  startActivityForResult(intent, REQUEST_CODE);
            		  } catch (Exception e) {
            		  Log.e("tag", e.getMessage());
            		  }
            		  progressDialog.dismiss(); //Dismiss the progress dialog
            		  }
            		  }.start();
            	   }	   
            	    else if (res.equals("0"))//Jika Username dan Password yang dimasukkan salah
            	    {	warning.setText("Username/Password anda salah!");}
            	    else if (res.equals("2"))
            	    {	warning.setText("Username/Password anda salah!!");}	
            	  
            	} catch (Exception e)
            		{ username.setText(e.toString());}
            	
           }    
      });  
        
    }
     else {  
    	
    	Toast t = Toast.makeText(this,"Tidak ada koneksi internet!!",8000);
        t.show();
        //{ progressDialog = ProgressDialog.show(login.this, "", "Mohon tunggu...");}
        { startActivity(new Intent(login.this, MIDDASActivity.class)); }

}
    
    
    }
}



